package com.example.user.customviewdemo.drawing.colorfilter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * 或许你身边一个不起眼的小人物就是某个领域的超级大神~不要轻易地侮辱任何一个人的错误，因为你也曾范错过
 * Created by user on 2016/5/4.
 * PorterDuffColorFilter可以实现bitmap和一个特定颜色的合成
 */
public class PorterDuffColorFilterView extends View {
    private Paint paint1,paint2,paint3,paint4,paint5,paint6;
    private Bitmap bm;

    public PorterDuffColorFilterView(Context context) {
        super(context);
    }

    public PorterDuffColorFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint1 = getPaint();
        paint2 = getPaint();
        paint3 = getPaint();
        paint4 = getPaint();
        paint5 = getPaint();
        paint6 = getPaint();

        bm = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
    }

    public PorterDuffColorFilterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.argb(255,41,49,52));

        int width  = 500;
        int height = width * bm.getHeight()/bm.getWidth();

        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint1);
        paint1.setColor(Color.BLACK);
        paint1.setTextSize(50);
        canvas.drawText("原图",100,400, paint1);
        canvas.save();

        /********Mode.MULTIPLY正片叠底***********/
        canvas.translate(510,0);
        paint2.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY));
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint2);
        paint2.setColor(Color.BLACK);
        paint2.setTextSize(50);
        canvas.drawText("正片叠底",100,400, paint1);

        /*********饱和度相加*******/
        canvas.restore();
        canvas.translate(0,500);
        paint3.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.ADD));
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint3);
        paint2.setColor(Color.BLACK);
        paint2.setTextSize(50);
        canvas.drawText("饱和度相加",100,400, paint1);
        canvas.save();

        /**********Model.DARKEN变暗*********/
        canvas.translate(510,0);
        paint4.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.DARKEN));
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint4);
        paint1.setColor(Color.BLACK);
        paint1.setTextSize(50);
        canvas.drawText("变暗",100,400, paint1);

        /*******Mode.LIGHTING变亮********/
        canvas.restore();
        canvas.translate(0,500);
        paint5.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN));
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint5);
        paint1.setColor(Color.BLACK);
        paint1.setTextSize(50);
        canvas.drawText("变亮",100,400, paint1);
        canvas.save();

        /******Mode.OVERLAY叠加*********/
        canvas.translate(510,0);
        paint6.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.OVERLAY));
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint6);
        paint1.setColor(Color.BLACK);
        paint1.setTextSize(50);
        canvas.drawText("叠加",100,400, paint1);

    }

    private Paint getPaint(){
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        return paint;
    }

}
